package com.xiaoshuidi.cloud.module.report.controller.finance;


import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.*;
import com.xiaoshuidi.cloud.module.report.convert.finance.ReportContractRoomDepositConvert;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.ReportContractRoomDepositDO;
import com.xiaoshuidi.cloud.module.report.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.report.service.ajreport.ReportContractRoomDepositService;
import com.xiaoshuidi.cloud.module.report.service.finance.DepositDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * 租赁押金报表
 */

@Slf4j
@Tag(name = "管理后台 -租赁押金报表")
@RestController
@RequestMapping(ApiConstants.PREFIX + "/contract-room/deposit")
public class ReportContractRoomDepositController {

    @Resource
    private ReportContractRoomDepositService reportContractRoomDepositService;


    /**
     * 租赁押金报表分页查询
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "租赁押金报表分页查询")
    public CommonResult<ContractRoomDepositRespVO> page(@RequestBody @Valid ContractRoomDepositReqVO reqVO) {
        log.info("DepositDetailReportController page req{}", reqVO.toString());
        return success(reportContractRoomDepositService.page(reqVO));
    }


    /**
     * 租赁押金报表出
     *
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, ContractRoomDepositReqVO reqVO) throws IOException {
        reqVO.setPageSize(100);
        LoginUser loginUser = getLoginUser();
        if (ObjectUtil.isNotNull(loginUser)) {
            reqVO.setTenantId(loginUser.getTenantId());
        }
        PageResult<ReportContractRoomDepositDO> pageResult = reportContractRoomDepositService.pageList(reqVO);
        ExcelUtils.write(response, "租赁押金明细.xlsx", "流水明细", ContractRoomDepositExportRespVO.class, pageResult,
                ReportContractRoomDepositConvert.INSTANCE::convertPageList, (pageNo) -> {
                    reqVO.setPageNo(pageNo);
                    return reportContractRoomDepositService.pageList(reqVO);
                });
    }

}
